package me.algo.coll2;

/**
 * @author guozheng
 * @date 2024/3/19
 */
public class LinkedLst<E> {

    public static void main(String[] args) {
        LinkedLst<Integer> lst = new LinkedLst<>();
        lst.addLast(1);
        lst.addLast(2);
        lst.addLast(3);
        lst.addLast(4);
        System.out.println(lst);
    }

    private Node<E> first;

    private Node<E> last;

    private int size;

    public void addFirst(E val) {
        Node<E> node = new Node<>(val);
        if (size == 0) {
            last = node;
            first = node;
        } else {
            first.prev=node;
            node.next = first;
            first = node;
        }
        size++;
    }
    public void addLast(E val) {
        Node<E> e = new Node<>(val);
        if (size == 0) {
            last = e;
            first = e;
        } else {
            last.next = e;
            e.prev = last;
            last = e;
        }
        size++;
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public LinkedLst() {
        this.size = 0;
    }

    private static class Node<E> {
        E val;
        Node<E> prev;
        Node<E> next;

        public Node(E val, Node<E> prev, Node<E> next) {
            this.val = val;
            this.prev = prev;
            this.next = next;
        }

        public Node(E val) {
            this.val = val;
        }
    }


}
